package com.zhonglz.support.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.zhonglz.model.SysFileInfo;
import com.zhonglz.service.SysFileInfoService;
import com.zhonglz.support.event.SealSignDataEvent;
import com.zhonglz.support.model.SealSignData;
import com.zhonglz.util.minio.MinioProperties;
import com.zhonglz.util.minio.MinioUtils;
import com.zhonglz.util.tools.ObjectUtils;
import com.zhonglz.util.tools.UuidUtils;

/***
 * PDF签署文件监听
 * @author zhonglz
 *
 */
@Component
public class FileSignListener {

	private static Logger logger = LoggerFactory.getLogger(FileSignListener.class);
	
	@Autowired
	private SysFileInfoService sysFileInfoService;
	
	@Order(30)
    @EventListener
    public void acceptEvent(SealSignDataEvent event) {
		logger.info("***************PDF签署文件监听开始********************");
		SealSignData sealSignData =event.getSealSignData();
		SysFileInfo sysFileInfo =sysFileInfoService.selectByPrimaryKey(sealSignData.getBuSignInfo().getSignFrontFileId());
		if(ObjectUtils.isEmpty(sysFileInfo)) {
			throw new RuntimeException("签署文件信息获取失败");
		}
		try {
			sealSignData.setSignFrontFile(MinioUtils.getObjectBytes(sysFileInfo.getFileAddress()));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("签署文件获取失败");
		}
		// 定义一个文件签署地址
		String fileUid = UuidUtils.uuid();
		String fileName =fileUid+".pdf";
		String filePath = MinioProperties.serverPath+"\\"+fileName;
		logger.info("设置签署后的文件路径："+filePath);
		sealSignData.setSignBackFileUid(fileUid);
		sealSignData.setSignBackFile(filePath);
		event.setSealSignData(sealSignData);
		logger.info("***************PDF签署文件监听结束********************");
	}
}
